//获取用户名
function initAdminInfo() {
	$.ajax({
			url:'queryLoginAdminName',
			type:'get',
			async: true,
			data: {},
			dataType: 'JSON',
			success: function(resp) {
				console.log(resp);
				var welcome = document.getElementById('welcome');
				welcome.innerHTML = '欢迎：'+resp.message;
			},
			error: function(err) {
				alert('请联系管理员...');
			}
	})
}
initAdminInfo();

//注销
function admExit() {
	var dt = confirm("确定退出?");
	if(dt==true){
		window.location.href = "admExit";
	}
	
}

function initMenu() {
	$.ajax({
		url:'SearchMenuByRoleId',
		type:'POST',
		async: true,
		data: {},
		dataType: 'JSON',
		success: function(resp) {
			console.log(resp);
			var arr = resp.datas.menu;
			var menus = [];
			//找到一级菜单，添加到数组
			for (var i = 0; i < arr.length; i++){
				if (arr[i].menuPid == 0){
					menus.push(arr[i]);
				}
			}
			var menus2 = [];
			//遍历一级菜单的数组
			for (var i = 0; i < menus.length; i++){
				var menuItems = [];
				//找到所有对应一级菜单的二级菜单
				for (var j = 0;j < arr.length;j++){
					if (menus[i].menuId == arr[j].menuPid){
						menuItems.push(arr[j]);
					}
				}
				menus2.push(menuItems);
			}
			//将数据渲染到界面上
			var str = '';
			for (var i = 0;i < menus.length; i++){
				str += '<div class="item">';
				str += '<div class="first" onclick="changeFirst(this)">'+menus[i].menuName+'</div>';
				for(var j = 0;j<menus2[i].length;j++){
					str+=`<div class="second" onclick="changSecond('${menus2[i][j].menuUrl}')">`+menus2[i][j].menuName+`</div>`
				}
				str +=	'</div>';
			}
			document.getElementById('left').innerHTML = str;
		},
		error: function(err) {
			alert('请联系管理员...');
		}
	})
}
initMenu();


function changeFirst(obj){
	//1.将所有的second的div都隐藏
	var seconds = document.getElementsByClassName('second');
	for (var i = 0;i<seconds.length;i++) {
		seconds[i].style.display="none";
	}
	//2.将当前obj下的second的div显示出来
	var arr = obj.parentElement.children;
	for (var i = 0;i<arr.length;i++) {
		arr[i].style.display="flex";
	}
}

function changSecond(loc){
	var change = document.getElementById('change');
	change.src = loc + ".jsp";
}
